//
//  QKEssenceController.m
//  Gigle
//
//  Created by 丁乾坤 on 16/9/9.
//  Copyright © 2016年 丁乾坤. All rights reserved.
//
                                /*******   精选   *******/

#import "QKEssenceController.h"
#import "NinaPagerView.h"
#import "UIView+QKFrame.h"
#import "QKTopicCell.h"
#import "QKTopicModel.h"
#import "QKSegmentView.h"
#import "QKRecommandController.h"
#import "QKVideoController.h"
#import "QKImageController.h"
#import "QKJokesController.h"



@interface QKEssenceController ()<UIScrollViewDelegate>

@property(nonatomic, strong)NSMutableArray *dataSource; //数据源

@property(nonatomic, strong)QKSegmentView *segmentView;

@property(nonatomic, strong)UIScrollView *scrollView;

@end

@implementation QKEssenceController

//单元格表示符
static NSString *cellIdentifier = @"QKTopicCell";

- (void)viewDidLoad {
    [super viewDidLoad];
    [self _setUpChildController];
    [self _setUpTitleViewAndScroll];
    [self _setUpControllerViewForScrollerView];
}
#pragma mark 初始化试图
- (void)_setUpTitleViewAndScroll{
    //加载标题视图
    weakSelf(self);
    QKSegmentView *segment = [QKSegmentView qk_segmentWithTitleArray:@[@"推荐", @"视频", @"图片", @"段子"] segmentSelectIndex:^(NSInteger index, NSString *title) {
        NSLog(@"%@,   %zd", title, index);
        [weakSelf.scrollView setContentOffset:CGPointMake(index * SWIDTH, 0) animated:YES];
    }];
    segment.frame = CGRectMake(0, 0, SWIDTH, QKTopSegmentHeight);
    segment.backgroundColor = [UIColor redColor];
    self.segmentView = segment;
    [self.view addSubview:segment];
    //加载ScrollView滚动视图
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, QKTopSegmentHeight, SWIDTH, SHEIGHT)];
    scrollView.pagingEnabled = YES;
    self.scrollView = scrollView;
    scrollView.delegate = self;
    [self.view addSubview:scrollView];
    [scrollView setContentSize:CGSizeMake(scrollView.qk_width * self.childViewControllers.count, scrollView.qk_height)];
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.showsHorizontalScrollIndicator = NO;
}

//添加子视图控制器
- (void)_setUpChildController{
    QKRecommandController *reconmmandVC = [[QKRecommandController alloc] init];
    [self addChildViewController:reconmmandVC];
    QKVideoController *videoVC = [[QKVideoController alloc] init];
    [self addChildViewController:videoVC];
    QKImageController *imageVC = [[QKImageController alloc] init];
    [self addChildViewController:imageVC];
    QKJokesController *jokesVC = [[QKJokesController alloc] init];
    [self addChildViewController:jokesVC];
}


//添加仕途控制器视图
- (void)_setUpControllerViewForScrollerView{
    NSInteger currentPage = self.scrollView.contentOffset.x / _scrollView.qk_width;
    UIViewController *currentVC = self.childViewControllers[currentPage];
     currentVC.view.frame = CGRectMake(currentPage * self.scrollView.qk_width, 0, self.scrollView.qk_width, self.scrollView.qk_height);
    [self.scrollView addSubview:currentVC.view];
}


#pragma mark ---  代理方法
//手动滑动时调用此方法改变字体的大小
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
        NSInteger i = scrollView.contentOffset.x / SWIDTH;
        UIButton *button = (UIButton *)self.segmentView.subviews[i];
        [self.segmentView changeFontSize:button];
}
//点击按钮时调用此方法 
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
        NSInteger i = scrollView.contentOffset.x / SWIDTH;
        UIViewController *viewController = self.childViewControllers[i];
        UIButton *button = (UIButton *)self.segmentView.subviews[i];
        [self.segmentView changeFontSize:button];
        if (!viewController.view.superview) {
            [self _setUpControllerViewForScrollerView];
        }
}


@end
